package com.linkage.lcims.base.dao.enterprise.impl;

import java.util.List;

import com.linkage.lcims.base.dao.HibernateBaseDAOImpl;
import com.linkage.lcims.base.dao.enterprise.interfaces.BVpdnTunnelDAO;
import com.linkage.lcims.base.po.enterprise.BVpdn;
import com.linkage.lcims.base.po.enterprise.BVpdnTunnel;
import com.linkage.util.exception.DAOException;

@SuppressWarnings("unchecked")
public class BVpdnTunnelDAOImpl extends HibernateBaseDAOImpl implements BVpdnTunnelDAO{

	public BVpdnTunnelDAOImpl() {
		super();
	}
	
	public BVpdn getRealm(String realm) throws DAOException {
		return (BVpdn) this.getByID(BVpdn.class, realm);
	}
	
	public List<BVpdnTunnel> getByRealmAndAreano(String realm, String areano)
			throws DAOException {
		try {
			String hql = "from BVpdnTunnel vpdnTunnel where realm = '" + realm
					+ "' and areano = '" + areano + "'";
			List list = query(hql);
			if (list == null || list.size() == 0) {
				return null;
			} else {
				return list;
			}
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}
	
	public void changePreference(BVpdnTunnel tunnel) throws DAOException {
		try {
			String hql = "update B_VPDN_TUNNEL set preference = 0 where realm = '"
					+ tunnel.getComp_id().getRealm()
					+ "' and areano ='"
					+ tunnel.getComp_id().getAreano() + "'";

			this.updateBySql(hql);
			this.merge(tunnel);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	public void deleteTunnelByRealmAndAreano(String realm, String areano)
			throws DAOException {
		try {
			String hql = "delete B_VPDN_TUNNEL where realm = '" + realm
					+ "' and areano ='" + areano + "'";
			this.deleteBySql(hql);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}
	
	public void changeTunnelIP(BVpdnTunnel tunnel, String oldTunnelip)
			throws DAOException {
		try {
			String hqlforinsert = "insert into B_VPDN_TUNNEL values('"
					+ tunnel.getComp_id().getRealm() + "','"
					+ tunnel.getComp_id().getAreano() + "','"
					+ tunnel.getComp_id().getTunnelip() + "','"
					+ tunnel.getTunnelpassword() + "',"
					+ tunnel.getPreference() + ")";
			String hqlfordelete = "delete from B_VPDN_TUNNEL where realm ='"
					+ tunnel.getComp_id().getRealm() + "' and areano ='"
					+ tunnel.getComp_id().getAreano() + "' and tunnelip ='"
					+ oldTunnelip + "'";
			this.insertBySql(hqlforinsert);
			this.deleteBySql(hqlfordelete);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	public List getBoundAreanoByRealm(String realm) throws DAOException {
		try {
			String hql = "from BVpdnTunnel vpdnTunnel where realm = '" + realm
					+ "'";
			List list = query(hql);
			if (list == null || list.size() == 0) {
				return null;
			} else {
				return list;
			}
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}
	
	
	public void resetPreference(BVpdnTunnel tunnel) throws DAOException {
		try {
			String hql = "update B_VPDN_TUNNEL set preference = 1 where realm = '"
					+ tunnel.getComp_id().getRealm()
					+ "' and areano ='"
					+ tunnel.getComp_id().getAreano() + "'";
			this.updateBySql(hql);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}
	
	public BVpdnTunnel getVpdnTunnelByID(BVpdnTunnel vpdnTunnel)
			throws DAOException {
		return (BVpdnTunnel)this.getByID(BVpdnTunnel.class, vpdnTunnel.getComp_id());
	}
	
	public List getAll()throws DAOException {
		return this.getAll(BVpdnTunnel.class);
	}

	

	public void deleteTunnel(BVpdnTunnel tunnel) throws DAOException {
		this.delete(BVpdnTunnel.class,tunnel.getComp_id());
	}

	
	
	public void saveTunnel(BVpdnTunnel tunnel) throws DAOException {
		this.save(tunnel);
	}


	public void updateTunnel(BVpdnTunnel tunnel) throws DAOException {
		this.merge(tunnel);
	}


	
	
}
